package demo1;

import java.util.Arrays;

public class MyArrayList implements IList {
    private int[] array;
    private int usedsize;
    public static final int CAPACITY = 10;
    public MyArrayList(){
        this.array = new int[CAPACITY];
    }
    @Override
    public void add(int data) {
        if(isFull()){
            growSize();
        }
        array[usedsize] = data;
        usedsize++;
    }

    public void growSize(){
        this.array = Arrays.copyOf(array,2*array.length);
    }

    public boolean isFull(){
        return this.array.length == usedsize;
    }

    public void checkPos(int pos) throws PosIllegal{
        if(pos < 0 || pos >usedsize){
            throw new PosIllegal("插入位置不合法");
        }
    }
    @Override
    public void add(int pos, int data) {
        try{
            checkPos(pos);
            if(isFull()){
                growSize();
            }
            int n = usedsize - 1;
            while(n >= pos){
                array[n + 1] = array[n];
                n--;
            }
            array[pos] = data;
            usedsize++;
        }catch(PosIllegal e){
            e.printStackTrace();
        }
    }

    @Override
    public boolean contains(int toFind) {
        for (int i = 0; i < usedsize; i++) {
            if(array[i] == toFind){
                return true;
            }
        }

        return false;
    }

    @Override
    public int indexOf(int toFind) {
        for (int i = 0; i < usedsize; i++) {
            if(array[i] == toFind){
                return i;
            }
        }
        return -1;
    }

    public void isEmpty ()throws EmptyException{
        if(this.usedsize == 0){
            throw new EmptyException("目标为空表");
        }
    }

    private void checkPos2(int pos)throws PosIllegal{
        if(pos < 0 || pos >=usedsize){
            throw new PosIllegal("目标位置不合法");
        }
    }
    @Override
    public int get(int pos) {
        try{
            isEmpty();
            checkPos2(pos);
            return array[pos];

        }catch(PosIllegal e){
            e.printStackTrace();
        }catch(EmptyException e){
            e.printStackTrace();
        }
        return -1;
    }

    @Override
    public void set(int pos, int value) {
        try{
            isEmpty();
            checkPos2(pos);
            array[pos] = value;

        }catch(PosIllegal e){
            e.printStackTrace();
        }catch(EmptyException e){
            e.printStackTrace();
        }

    }

    @Override
    public void remove(int toRemove) {
        try{
            isEmpty();
            int index = indexOf(toRemove);
            if(index == -1){
                return;
            }
            for (int i = index; i < usedsize - 1; i++) {
                array[i] = array[i + 1];
            }
            usedsize--;
        }catch(EmptyException e){
            e.printStackTrace();
        }
    }

    @Override
    public int size() {
        return this.usedsize;
    }

    @Override
    public void clear() {
        this.usedsize = 0;
    }

    @Override
    public void display() {
        for (int i = 0; i < usedsize; i++) {
            System.out.print(array[i] + " ");
        }
        System.out.println();
    }
}
